package xymt.novaway.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import xymt.novaway.common.idempotent.annotation.RepeatSubmit;
import xymt.novaway.common.log.annotation.Log;
import xymt.novaway.common.web.core.BaseController;
import xymt.novaway.common.mybatis.core.page.PageQuery;
import xymt.novaway.common.core.domain.R;
import xymt.novaway.common.core.validate.AddGroup;
import xymt.novaway.common.core.validate.EditGroup;
import xymt.novaway.common.log.enums.BusinessType;
import xymt.novaway.common.excel.utils.ExcelUtil;
import xymt.novaway.system.domain.vo.MyProVo;
import xymt.novaway.system.domain.bo.MyProBo;
import xymt.novaway.system.service.IMyProService;
import xymt.novaway.common.mybatis.core.page.TableDataInfo;

/**
 * 功能
 *
 * @author NovaWay
 * @date 2025-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/pro")
public class MyProController extends BaseController {

    private final IMyProService myProService;

    /**
     * 查询功能列表
     */
    @SaCheckPermission("system:pro:list")
    @GetMapping("/list")
    public TableDataInfo<MyProVo> list(MyProBo bo, PageQuery pageQuery) {
        return myProService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出功能列表
     */
    @SaCheckPermission("system:pro:export")
    @Log(title = "功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MyProBo bo, HttpServletResponse response) {
        List<MyProVo> list = myProService.queryList(bo);
        ExcelUtil.exportExcel(list, "功能", MyProVo.class, response);
    }

    /**
     * 获取功能详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:pro:query")
    @GetMapping("/{id}")
    public R<MyProVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(myProService.queryById(id));
    }

    /**
     * 新增功能
     */
    @SaCheckPermission("system:pro:add")
    @Log(title = "功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MyProBo bo) {
        return toAjax(myProService.insertByBo(bo));
    }

    /**
     * 修改功能
     */
    @SaCheckPermission("system:pro:edit")
    @Log(title = "功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MyProBo bo) {
        return toAjax(myProService.updateByBo(bo));
    }

    /**
     * 删除功能
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:pro:remove")
    @Log(title = "功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(myProService.deleteWithValidByIds(List.of(ids), true));
    }
}
